/*
 * @filename CORSConfiguration.java
 * @author barry
 * @version 0.0.1
 * @date 2020年3月13日
 */
package com.bnzj.core.web.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * Spring Security的跨域配置，必须要有这个CorsFilter
 * @author barry
 * @date 2020-03-13
 */
@Configuration
public class GlobalCorsSourceConfiguration {

    @ConditionalOnProperty(prefix = "bnzj.cors.filter", name = "enabled", havingValue = "true", matchIfMissing = true)
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration cors = new CorsConfiguration();
        cors.setAllowCredentials(true);
        cors.addAllowedHeader("*");
        cors.addAllowedOrigin("*");
        cors.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource configSource =new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", cors);
        return new CorsFilter(configSource);
    }
}
